package network;

import gui.MainPanel;
import gui.SuperUserPanel;

import java.util.concurrent.ExecutionException;

import javax.swing.JPanel;
import javax.swing.SwingWorker;

import data.OrderResponse;


public class GetOrderWorker extends SwingWorker<OrderResponse, Object> {
	private int mDbId;
	private JPanel mParentPanel;
	
	public GetOrderWorker(int dbId, JPanel mainPanel) {
		mDbId = dbId;
		mParentPanel = mainPanel;
	}
	
	@Override
	protected OrderResponse doInBackground() throws Exception {
		return NetworkManager.getInstance().getOrder(mDbId);
	}

	@Override
	protected void done() {
		OrderResponse response = null;
		try {
			response = get();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (mParentPanel instanceof MainPanel)
				((MainPanel)mParentPanel).orderLoaded(response);
			else if (mParentPanel instanceof SuperUserPanel)
				((SuperUserPanel)mParentPanel).orderLoaded(response);
		}
		super.done();
	}
	
}
